package com.smart.electronic.fence.controller;

import com.alibaba.fastjson.JSONObject;
import com.smart.electronic.fence.pojo.User;
import com.smart.electronic.fence.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping(value = "/login")
    public JSONObject login( @RequestParam String userName,@RequestParam String userPassword){
        if (!userName.isEmpty()&&!userPassword.isEmpty() ){
            User user = userService.getByUserNameAndPwd(userName,userPassword);
            JSONObject obj = new JSONObject();
            if (user!=null){
                user.setOnline(1);
                userService.editOnlineByUser(user);
                obj.put("code",200);
                obj.put("data",user);
                System.out.println("登陆成功");
            }else{
                obj.put("cpde",400);
                obj.put("data",user);
                System.out.println("登陆成功");
            }
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("登陆失败");
            return obj;
        }



    }

    @PostMapping(value = "/sign_up")
    public JSONObject signUp(@RequestParam String userName,@RequestParam String userPassword){
        if (!userName.isEmpty()&&!userPassword.isEmpty() ){
            User user  = new User();
            user.setUserName(userName);
            user.setUserPassword(userPassword);
            user.setOnline(0);
            JSONObject obj = new JSONObject();
            userService.addUserInfo(user);
            obj.put("code",200);
            obj.put("data",userService.getByUserNameAndPwd(userName,userPassword));
            System.out.println("注册成功");
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("注册失败");
            return obj;
        }

    }

    @PostMapping(value = "/exit")
    public JSONObject exitByUserNameAndPwd(@RequestBody User user){
        if (user!=null){
            user.setOnline(0);
            userService.exitByUserNameAndPwd(user);
            JSONObject obj = new JSONObject();
            obj.put("code",200);
            obj.put("data","");
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("失败");
            return obj;
        }

    }

    @PostMapping(value = "/add")
    public JSONObject addUserPwdByUserName(@RequestBody User user){
        if (user!=null){
            user.setOnline(0);
            int status = userService.addUserInfo(user);
            JSONObject obj = new JSONObject();
            obj.put("code",status == 1? 200:400);
            obj.put("data","");
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("失败");
            return obj;
        }
    }

    @PostMapping(value = "/editPwd")
    public JSONObject editUserPwdByUserName(@RequestParam String userName,@RequestParam String userPassword){
        if (!userName.isEmpty()&&!userPassword.isEmpty()){
            User user = new User();
            user.setUserName(userName);
            user.setUserPassword(userPassword);
            userService.editPwd(user);
            JSONObject obj = new JSONObject();
            obj.put("code",200);
            obj.put("data",userService.getByUserNameAndPwd(userName,userPassword));
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("失败");
            return obj;
        }

    }

    @PostMapping(value = "/edit")
    public JSONObject editUserPwdByUserName(@RequestBody User user){
        if(user!=null){
            userService.editById(user);
            JSONObject obj = new JSONObject();
            obj.put("code",200);
            obj.put("data",userService.getById(user.getUserId()));
            return obj;
        }else{
            JSONObject obj = new JSONObject();
            obj.put("cpde",400);
            obj.put("data","");
            System.out.println("失败");
            return obj;
        }

    }


    @PostMapping(value = "/detail")
    public JSONObject getUserDetailById(@RequestParam BigInteger userId){
        User user = userService.getById(userId);
        JSONObject obj = new JSONObject();
        obj.put("code",200);
        obj.put("data",user);
        return obj;
    }


    
    
}
